
const peer_res = {};
const res_peer = {};
const peer_online = {};
const peer_video = {};
const resType = {
    cdn: 0,
    p2p: 0
}

export default class Resources {

    add(socketid, videoid, resource, type){
        if(type === 1){
            resType.p2p++;
        }else{
            resType.cdn++;
        }
        peer_video[socketid] = videoid;

        res_peer[videoid] = res_peer[videoid] || {};

        peer_res[socketid] = peer_res[socketid] || [];

        res_peer[videoid][resource] = res_peer[videoid][resource] || [];

        if (peer_res[socketid] && peer_res[socketid].indexOf(resource) >= 0) return;

        if (res_peer[videoid][resource] && res_peer[videoid][resource].indexOf(socketid) >= 0) return;

        peer_res[socketid].push(resource);

        // 优先调用新资源，所以从头部插入
        res_peer[videoid][resource] = [].concat(socketid, res_peer[videoid][resource]);


        // console.log(res_peer);

    }

    get(videoid, resource){
        if(res_peer[videoid] && res_peer[videoid][resource])
            return res_peer[videoid][resource].slice(0, 3);
        else
            return [];
    }


    delete(socketid){
        if(peer_res[socketid] && peer_res[socketid].length > 0)
            peer_res[socketid].forEach(function(res) {
                res_peer[peer_video[socketid]][res].splice(res_peer[peer_video[socketid]][res].indexOf(res), 1);
            }, this);
        delete peer_res[socketid];
    }

    getType(){
        return resType;
    }


}